﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 计算器
{
    public partial class Q : Form
    {
        public List<Ques> ql = new List<Ques>();
        public List<int> oplist; ///////1.加法  2.减法  3.乘法   4.除法
        public Form1 father;
        public Q( )
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void Q_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            timer.Start();
            Random r = new Random();
            int i = 0;
            while(i<father.questionnum)
            {
                int j = i % 15;
                int k = (i + 15 - j) / 15-1;
                int opr = r.Next(1, 5);
                if (opr == 1 && father.addcheck == true)
                {
                    int a = r.Next(father.addmin, father.addmax + 1);
                    int b = r.Next(father.addmin, father.addmax + 1);
                    int right = a + b;
                    if (right <= father.addrmax && right >= father.addrmin)
                    {
                        Ques q = new Ques();
                        this.Controls.Add(q.qulabel);
                        q.qulabel.Location = new System.Drawing.Point(100 * k + 10, 20 * j + 10);
                        q.qulabel.Size = new System.Drawing.Size(50, 20);
                        q.qulabel.Text = a.ToString() + "+" + b.ToString() + "=";
                        this.Controls.Add(q.youranswer);
                        q.youranswer.Location = new System.Drawing.Point(100 * k + 60, 20 * j + 10);
                        q.youranswer.Size = new System.Drawing.Size(40, 18);
                        q.rightanswer = right;
                        ql.Add(q);
                        i++;
                    }
                }
                else if (opr == 2 && father.subcheck == true)
                {
                    int a = r.Next(father.sub1min,father.sub1max+1);
                    int b = r.Next(father.sub2min, father.sub2max+1);
                    int right = a - b;
                    Ques q = new Ques();
                    this.Controls.Add(q.qulabel);
                    q.qulabel.Location = new System.Drawing.Point(100 * k + 10, 20 * j + 10);
                    q.qulabel.Size = new System.Drawing.Size(50, 20);
                    q.qulabel.Text = a.ToString() + "-" + b.ToString() + "=";
                    this.Controls.Add(q.youranswer);
                    q.youranswer.Location = new System.Drawing.Point(100 * k + 60, 20 * j + 10);
                    q.youranswer.Size = new System.Drawing.Size(40, 18);
                    q.rightanswer = right;


                    ql.Add(q);
                    i++;
                }
                else if (opr == 3 && father.mulcheck== true)
                {
                    int a = r.Next(father.mulmin,father.mulmax+1);
                    int b = r.Next(father.mulmin, father.mulmax + 1);
                    int right = a *b;
                    if (right >= father.mulrmin && right <= father.mulrmax)
                    {
                        Ques q = new Ques();
                        this.Controls.Add(q.qulabel);
                        q.qulabel.Location = new System.Drawing.Point(100 * k + 10, 20 * j + 10);
                        q.qulabel.Size = new System.Drawing.Size(50, 20);
                        q.qulabel.Text = a.ToString() + "*" + b.ToString() + "=";
                        this.Controls.Add(q.youranswer);
                        q.youranswer.Location = new System.Drawing.Point(100 * k + 60, 20 * j + 10);
                        q.youranswer.Size = new System.Drawing.Size(40, 18);
                        q.rightanswer = right;


                        ql.Add(q);
                        i++;
                    }
                }
                else if (opr == 4 && father.divcheck == true)
                {
                    int a = r.Next(father.div1min, father.div1max + 1);
                    int b = r.Next(father.div2min, father.div2max + 1);
                    int right = a / b;
                    Ques q = new Ques();
                    this.Controls.Add(q.qulabel);
                    q.qulabel.Location = new System.Drawing.Point(100 * k + 10, 20 * j + 10);
                    q.qulabel.Size = new System.Drawing.Size(50, 20);
                    q.qulabel.Text = a.ToString() + "/" + b.ToString() + "=";
                    this.Controls.Add(q.youranswer);
                    q.youranswer.Location = new System.Drawing.Point(100 * k + 60, 20 * j + 10);
                    q.youranswer.Size = new System.Drawing.Size(40, 18);
                    q.rightanswer = right;


                    ql.Add(q);
                    i++;
                }

               /* Label a = new Label();
                int iYvalue = i * 25;

                a.Location = new System.Drawing.Point(10, iYvalue);
                a.Text = "sd";
                this.Controls.Add(a);*/
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer.Stop();
            int wrong = 0;
            for (int i = 0; i < father.questionnum; i++)
            {
                if (ql[i].youranswer.Text.Trim().Length == 0)
                {
                    wrong++;
                    father.wrong++;
                    father.wronglist.Add(ql[i]);
                    ql[i].youranswer.BackColor = Color.Red;
                }
                else if (Convert.ToInt32(ql[i].youranswer.Text) != ql[i].rightanswer)
                {
                    wrong++;
                    father.wrong++;
                    father.wronglist.Add(ql[i]);
                    ql[i].youranswer.BackColor = Color.Red;
                }

            }
            string s = "共" + father.questionnum.ToString() + "题，答错" + wrong.ToString() + "题";
            MessageBox.Show(s);
            
        }
       TimeSpan ts = new TimeSpan(0, 0, 0);
        private void timer_Tick(object sender, EventArgs e)
        {
              int wrong = 0;
            ts = ts.Add(new TimeSpan(0, 0, 0,0,100));//每隔一秒增一秒
            String str = ts.Hours.ToString() + ":" + ts.Minutes.ToString() + ":" + ts.Seconds.ToString();// ts.Seconds.ToString();
            Timelabel.Text = str;//label17用来显示剩余的时间
            if (ts.TotalMinutes >father.mytime)//当倒计时完毕
            {
                timer.Stop();
                Timelabel.Text = "时间到";
              
                for (int i = 0; i < father.questionnum; i++)
                {
                    if (ql[i].youranswer.Text.Trim().Length == 0)
                    {
                        wrong++;
                        father.wrong++;
                        father.wronglist.Add(ql[i]);
                        ql[i].youranswer.BackColor = Color.Red;
                    }
                    else if (Convert.ToInt32(ql[i].youranswer.Text) != ql[i].rightanswer)
                    {
                        wrong++;
                        father.wrong++;
                        father.wronglist.Add(ql[i]);
                        ql[i].youranswer.BackColor = Color.Red;
                    }
                   
                }
                string s = "共" + father.questionnum.ToString() + "题，答错" + wrong.ToString() + "题";
                MessageBox.Show(s);
            }
           
        }

        private void button3_Click(object sender, EventArgs e)
        {
            timer.Stop();
            this.Close();
            return;
        }
    }
}
